// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

#ifndef FIRMWARE_HEADER_H_
#define FIRMWARE_HEADER_H_

#include <stdint.h>
#include "status/rot_status.h"
#include "common/image_header.h"
#include "flash/flash.h"


#define	FIRMWARE_HEADER_MARKER			0x43494d47
#define	FIRMWARE_HEADER_MAX_LENGTH		1024


/**
 * Interface for a firmware header that provides information about the image.
 */
struct firmware_header {
	struct image_header base;			/**< The base firmware header instance. */
};


int firmware_header_init (struct firmware_header *header, struct flash *flash, uint32_t addr);
void firmware_header_release (struct firmware_header *header);

int firmware_header_get_recovery_revision (struct firmware_header *header, int *revision);
int firmware_header_get_extra_images (struct firmware_header *header);
int firmware_header_get_earliest_allowed_revision (struct firmware_header *header, int *revision);
int firmware_header_get_signature_info (struct firmware_header *header, size_t *signed_length,
	size_t *sig_length);


#define	FIRMWARE_HEADER_ERROR(code)		ROT_ERROR (ROT_MODULE_FIRMWARE_HEADER, code)

/**
 * Error codes that can be generated by a firmware header.
 *
 * NOTE: Some error codes are deprecated in favor of IMAGE_HEADER error codes.
 */
enum {
	FIRMWARE_HEADER_INVALID_ARGUMENT = FIRMWARE_HEADER_ERROR (0x00),	/**< Input parameter is null or not valid. */
	FIRMWARE_HEADER_NO_MEMORY = FIRMWARE_HEADER_ERROR (0x01),			/**< Memory allocation failed. */
	//FIRMWARE_HEADER_NOT_MINIMUM_SIZE = FIRMWARE_HEADER_ERROR (0x02),	/**< The header length is not the minimum size. */
	//FIRMWARE_HEADER_TOO_LONG = FIRMWARE_HEADER_ERROR (0x03),			/**< The header length exceeds the maximum. */
	//FIRMWARE_HEADER_BAD_MARKER = FIRMWARE_HEADER_ERROR (0x04),		/**< The header marker is not valid. */
	FIRMWARE_HEADER_BAD_FORMAT_LENGTH = FIRMWARE_HEADER_ERROR (0x05),	/**< The header length doesn't match the expected length for the format. */
	FIRMWARE_HEADER_INFO_NOT_AVAILABLE = FIRMWARE_HEADER_ERROR (0x06),	/**< The requested information is not available in the header format. */
};


#endif /* FIRMWARE_HEADER_H_ */
